package com.lz.helper.face

import android.graphics.Bitmap
import com.face.FaceRecognize
import com.face.ImageUtils

/**
 * 人脸识别参数
 */
class FeatureParams(
    val bitmap: Bitmap,
    val threshold: Float
) {

    private var detect: IntArray? = null

    private var feature: FloatArray? = null

    var thresholdResult: Float = 0f

    fun isSuccess(): Boolean = thresholdResult > 0 && thresholdResult >= threshold

    private var hasInitParams = false

    @Synchronized
    private fun initParams() {
        if (hasInitParams) return
        hasInitParams = true
        val performBitmap = FaceRecognize.getInstance().getPerformSuperResolution(bitmap) ?: return
        val bytes = ImageUtils.bitmapToRGBAByteArray(performBitmap)
        detect = FaceRecognize.getInstance().detectFace(
            bytes, performBitmap.width, performBitmap.height, FaceRecognize.IMAGE_TYPE_RGBA
        )
        if (detect != null && detect!!.size > 1) {
            feature = FaceRecognize.getInstance().featureExtract(
                bytes, performBitmap.width, performBitmap.height, detect, FaceRecognize.IMAGE_TYPE_RGBA
            )
        }
        performBitmap.recycle()
    }

    internal fun detect(): IntArray? {
        initParams()
        return detect
    }

    internal fun feature(): FloatArray? {
        initParams()
        return feature
    }
}